import java.util.ArrayList;
import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * User:xieChao
 * Date:2022/10/1 15:24
 */

class MyArrayList<E> {
    private Object[] elementData;//数组
    private int useSize;//代表有效数据个数

    private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};//默认空数组
    public MyArrayList(){
        this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
    }

    private MyArrayList(int capacity){
        //对初始化容量参数进行判断
        if (capacity > 0) {
            this.elementData = new Object[capacity];
        } else if (capacity == 0) {
            this.elementData = new Object[0];//默认就是一个空数组，此处完善提醒
        } else {
            throw new IllegalArgumentException("初始化容量不能为负数！！！");
        }
    }

    /**
     * 添加元素
     * @param e
     * @return
     */
    public boolean add(E e){
        ensureCapacityInternal(useSize+1);//确定真正的容量
        elementData[useSize] = e;
        useSize++;
        return true;
    }
    private void ensureCapacityInternal(int minCapacity){
        int capacity = calculateCapacity(elementData,minCapacity);//计算出所需容量
        ensureExplicitCapacity(capacity);//判断是扩还是分配
    }
    private void ensureExplicitCapacity(int minCapacity) {
        // overflow-conscious code
        if (minCapacity - elementData.length > 0)
            //进入if语句就扩容了
            grow(minCapacity);
    }
    private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE-8;
    private void grow(int minCapacity) {
        // overflow-conscious code
        int oldCapacity = elementData.length;
        int newCapacity = oldCapacity + (oldCapacity >> 1);//1.5倍的扩容
        if (newCapacity - minCapacity < 0)
            newCapacity = minCapacity;
        if (newCapacity - MAX_ARRAY_SIZE > 0)
            //需要的容量非常大
            newCapacity = hugeCapacity(minCapacity);
        elementData = Arrays.copyOf(elementData, newCapacity);
    }

    /***
     * 巨大容量方法的实现
     * @param minCapacity
     * @return
     */
    private static int hugeCapacity(int minCapacity) {
        if (minCapacity < 0) // overflow
            throw new OutOfMemoryError();
        return (minCapacity > MAX_ARRAY_SIZE) ?
                Integer.MAX_VALUE :
                MAX_ARRAY_SIZE;
    }


    private static int calculateCapacity(Object[] elementData, int minCapacity) {
        //1.elementData数组之前是否分配过大小
        if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
            return Math.max(10, minCapacity);
        }
        //分配过后，返回+1后的值
        return minCapacity;
    }

    /**
     * 带参数的add实现
     * @param index
     * @param element
     * usesize可能写错
     */
    public void add(int index, E element) {
        rangeCheckForAdd(index);

        ensureCapacityInternal(useSize + 1);  // Increments modCount!!
        System.arraycopy(elementData, index, elementData, index + 1,
                useSize- index);
        elementData[index] = element;
        useSize++;
    }


    private void rangeCheckForAdd(int index) {
        if (index >useSize|| index < 0)
            throw new IndexOutOfBoundsException("位置不合法！！！");
    }
}


public class TestArrayList {
    public static void main(String[] args) {
        MyArrayList<String> list= new MyArrayList<>();
        list.add("x");
        list.add("i");
        list.add("e");
        list.add(1,"mm");
        System.out.println();
    }
}
